package com.yanqu.road.dao.impl.activity.daomu;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.daomu.data.DaoMuActivityUserCollectionData;
import com.yanqu.road.entity.activity.daomu.data.DaoMuActivityUserData;
import com.yanqu.road.entity.activity.daomu.data.DaoMuActivityUserOfferRewardData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DaoMuUserOfferRewardDataImpl extends TempDataObjectDao<DaoMuActivityUserOfferRewardData> {

    public static String selectSql = "select * from `t_u_activity_daomu_user_offer_reward_data` where `activityId`= ? and userId = ?";
    /**
     * 查询
     * @param activityId
     * @return
     */
    public Map<Integer, Map<Integer, DaoMuActivityUserOfferRewardData>> getDaoMuActivityUserOfferRewardData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer ,Map<Integer, DaoMuActivityUserOfferRewardData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                DaoMuActivityUserOfferRewardData data = parse(rs);
                Map<Integer, DaoMuActivityUserOfferRewardData> npcMap = map.get(data.getPassesId());
                if (npcMap == null) {
                    npcMap = new HashMap<>();
                }
                npcMap.put(data.getNpcId(), data);
                map.put(data.getPassesId(), npcMap);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertSql = "INSERT INTO `t_u_activity_daomu_user_offer_reward_data`(`activityId`, `userId`, `passesId`, `npcId`, `status`) VALUES (?, ?, ?, ?, ?)";
    /**
     * 插入
     * @param data 数据对象
     * @return
     */
    @Override
    protected boolean addData(DaoMuActivityUserOfferRewardData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassesId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNpcId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStatus()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE `t_u_activity_daomu_user_offer_reward_data` SET `passesId` = ?, `npcId` = ?, `status` = ? WHERE `activityId` = ? AND `userId` = ?";
    /**
     * 更新
     * @param data
     * @return
     */
    @Override
    protected boolean updateData(DaoMuActivityUserOfferRewardData data) {
        String newUpdateSql = "UPDATE `t_u_activity_daomu_user_offer_reward_data` SET  `status` = ? WHERE `activityId` = ? AND `userId` = ? AND `passesId` = ? AND `npcId` = ? ";
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassesId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNpcId()));
        if (getDb().executeNoneQuery(newUpdateSql, para) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 解析数据
     * @param rs
     * @return
     * @throws SQLException
     */
    @Override
    protected DaoMuActivityUserOfferRewardData parseData(ResultSet rs) throws SQLException {
        DaoMuActivityUserOfferRewardData data = new DaoMuActivityUserOfferRewardData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setPassesId(rs.getInt("passesId"));
        data.setNpcId(rs.getInt("npcId"));
        data.setStatus(rs.getInt("status"));
        return data;
    }
}
